package com.dbexplore.db;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DB {

    public Connection connection;
    public static final DB INSTANCE = new DB();

    private DB() {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream(new File("/Users/Admin/opensource/dbexplore/db.properties")));
            Class.forName(properties.getProperty("db.drivername"));
            String url = properties.getProperty("db.url");
            connection = DriverManager.getConnection(url, properties.getProperty("db.user"), properties.getProperty("db.password"));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public final void close() throws java.sql.SQLException {
        connection.commit();
    }

    public void pingDatabase() throws SQLException {
        Statement stmt = connection.createStatement();
        stmt.executeQuery("select 0 from dual");
        stmt.close();
    }
}